const Router = require("koa-router")
const {successRender, paginationFilter, errorRender} = require('../utils/utils')
const router = new Router()
const MenuType = require('../model/MenuType')
const Menu = require('../model/Menu')
const {proving} = require('../utils/token')


//查询所有菜单
router.get('/getMenu', async ctx => {
  let menuList
  const menuTypeList = await MenuType.find().lean()
  const keyWord = ctx.request.query.keyWord
  const typeId = ctx.request.query.typeId
  const reg = new RegExp(keyWord, 'i')
  if (typeId && keyWord) {
    menuList = await Menu.find({
      parentId: typeId,
      $or: [{title: {$regex: reg}}]
    }).lean()
  } else if (keyWord) {
    menuList = await Menu.find({
      $or: [{title: {$regex: reg}}]
    }).lean()
  } else if (typeId) {
    menuList = await Menu.find({parentId: typeId}).lean()
  } else {
    menuList = await Menu.find().lean()
  }
  if (ctx.query.pageSize || ctx.query.pageNum) {
    menuTypeList.forEach(menuType => {
      menuList.forEach(menu => {
        if (menu.parentId == menuType._id) {
          menu.parentName = menuType.title
        }
      })
    })
    const data = paginationFilter(menuList, ctx.request.query)
    ctx.body = successRender({data})
  } else {
    menuTypeList.forEach(menuType => {
      menuType.children = []
      menuList.forEach(menu => {
        if (menu.parentId == menuType._id) {
          menuType.children.push(menu)
        }
      })
    })
    ctx.body = successRender({data: menuTypeList})
  }
})

//查询菜单详情
router.get('/getMenuDetails', async ctx => {
  const _id = ctx.request.query.id
  const res = await Menu.findById(_id)
  ctx.body = successRender({data: res})
})

//查询菜单分类详情
router.get('/getMenuTypeDetails', async ctx => {
  const _id = ctx.request.query.id
  const res = await MenuType.findById(_id)
  ctx.body = successRender({data: res})
})

//查询菜单分类
router.get('/getMenuType', async ctx => {
  const keyWord = ctx.request.query.keyWord
  const reg = new RegExp(keyWord, 'i')
  let res
  if (keyWord) {
    res = await MenuType.find({$or: [{title: {$regex: reg}}]}).lean()
  } else {
    res = await MenuType.find().lean()
  }
  if (ctx.query.pageSize || ctx.query.pageNum) {
    const data = paginationFilter(res, ctx.request.query)
    ctx.body = successRender({data})
  } else {
    ctx.body = successRender({data: res})
  }
})

//添加菜单分类
router.post('/addMenuType', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const MenuTypeInfo = ctx.request.body
    const res = await new MenuType(MenuTypeInfo).save()
    ctx.body = successRender({msg: '添加成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//添加菜品
router.post('/addMenu', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const menuInfo = ctx.request.body
    const res = await new Menu(menuInfo).save()
    ctx.body = successRender({msg: '添加成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

// 修改菜单分类
router.post('/editMenuType', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const id = ctx.request.body._id
    const menuInfo = ctx.request.body
    const res = await MenuType.findByIdAndUpdate(id, menuInfo)
    ctx.body = successRender({msg: '修改成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

// 修改菜单
router.post('/editMenu', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const id = ctx.request.body._id
    const menuInfo = ctx.request.body
    const res = await Menu.findByIdAndUpdate(id, menuInfo)
    ctx.body = successRender({msg: '修改成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//删除菜品
router.get('/deleteMenu', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const _id = ctx.request.query.id
    const res = await Menu.findOneAndRemove({_id})
    ctx.body = successRender({msg: '删除成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//删除菜单分类
router.get('/deleteMenuType', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const _id = ctx.request.query.id
    const res1 = await MenuType.findOneAndRemove({_id})
    const res2 = await Menu.remove({parentId: {$in: _id}})
    ctx.body = successRender({msg: '删除成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//获取推荐菜品
router.get('/getRecommend', async ctx => {
  const res = await Menu.find({recommend: true})
  ctx.body = successRender({data: res})
})

module.exports = router
